<?php

namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\func\func;
use api\modules\v1\models\Publish;
use Yii;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;

class PublishController extends U
{
    public $modelClass = 'api\modules\v1\models\Publish';

    public function behaviors() {
        //if(in_array(Yii::$app->controller->action->id,['newswap'])){
          //  $this->_is_auth = false;
        //}
        $behaviors = parent::behaviors();
        return $behaviors;
    }


    public function actions()
    {
        return [
            'index' => [
                'class' => 'yii\rest\IndexAction',
                'modelClass' => $this->modelClass,
                'prepareDataProvider' => [$this, 'prepareDataProvider']
            ],
            'options' => [
                'class' => 'yii\rest\OptionsAction'
            ]
        ];
    }
    /*
     * 更换昵称
     * @param $id
     * @return mixed
     */
    public function actionImg(){
        $model = new $this->modelClass;
        $model->scenario='img';
        $model->attributes = Yii::$app->request->post();
        if ($img = $model->img()) {
            return [
                "url"=>$img,
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 文章发布
     * @return mixed
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionNews(){
        $model = new $this->modelClass();
        $model->scenario='publish';
        $model->attributes = Yii::$app->request->post();
        if($model->news()){
            return [
                "message"=>'文章发布成功!',
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }


    /*
     * 修改文章
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionEdit(){
        $id = Yii::$app->request->post('id');
        $model = $this->findModel($id);
        $model->scenario='edit';
        $model->attributes = Yii::$app->request->post();

        if($model->edit()){
            return [
                "message"=>'文章修改成功!',
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 文章列表
     */
    public function prepareDataProvider()
    {
        $modelClass = $this->modelClass;
        $searchModel = new $modelClass();
        $searchModel->scenario='search';
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
        return $dataProvider;
    }

    /*
     * 文章详情
     */
    public function actionInfo($id){
        $modelClass = $this->modelClass;
        $Model = new $modelClass();
        return $Model->getInfo(\Yii::$app->request->queryParams);
    }

    /*
     * 文章删除
     * @param $id
     * @return array
     * @throws HttpException
     * @throws ServerErrorHttpException
     */
    public function actionDel($id){
        $model = new $this->modelClass();
        if($model->del($id)){
            return [
                "message"=>'文章删除成功!',
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 文章统计
     */
    public function actionStatistic(){
        $model = new $this->modelClass();
        $data = $model->statistic();
        if(is_array($data)){
            return $data;
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 文章预览
     */
    public function actionPreview(){
        $params = \Yii::$app->request->bodyParams;
        if(empty($params['title'])) throw new HttpException(401,'标题不能为空',0);
        if(empty($params['content'])) throw new HttpException(401,'内容不能为空',0);
        $title = $params['title'];
        $content = $params['content'];

        func::filterEmoji($title,'标题');
        func::filterEmoji($content,'内容');

        $content = str_replace('<p></p>','<p>&nbsp;</p>',$content);
        $time = date('Y-m-d');
        $html = <<<EOT
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/html"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=1,target-densitydpi=medium-dpi"/><meta name="format-detection" content="telephone=no" /><meta name="mobileoptimized" content="0"><title>{$title}</title><link rel="stylesheet" href="http://m.rm1588.com/statics/css/mobile/public.css" /><link rel="stylesheet" href="http://m.rm1588.com/statics/css/mobile/theme.css" /><link rel="stylesheet" href="http://m.rm1588.com/statics/css/mobile/content.css" /><link rel="stylesheet" href="http://m.rm1588.com/statics/css/mobile/header.css" /><link rel="stylesheet" href="http://m.rm1588.com/statics/css/mobile/enjoy.css" /><link rel="stylesheet" href="http://m.rm1588.com/statics/css/mobile/music.css" /><style>body{background: #ffffff} .enjoy_s { width: 62%;}@media only screen and (min-width: 320px) and (max-width: 374px) {.enjoy_s { width: 80%;}}@media only screen and (min-width: 375px) and (max-width: 413px) {.enjoy_s { width: 68%;}}</style></head><body><section class="mlrAuto in_con"><div class="con_top"><h1><b>{$title}</b></h1><div class="meta"><span class="time">{$time}</span><span class="contentbar"></span></div></div><div class="con_con">{$content}</div><div class="detail_page" ></div></section></body></html>
EOT;
return $html;
    }

}
